package cn.iocoder.yudao.module.wecom.controller.admin.chatgroupmember;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wecom.controller.admin.chatgroupmember.vo.*;
import cn.iocoder.yudao.module.wecom.dal.dataobject.chatgroupmember.WeChatGroupMemberDO;
import cn.iocoder.yudao.module.wecom.service.chatgroupmember.WeChatGroupMemberService;

@Tag(name = "管理后台 - 客户群成员")
@RestController
@RequestMapping("/wecom/we-chat-group-member")
@Validated
public class WeChatGroupMemberController {

    @Resource
    private WeChatGroupMemberService weChatGroupMemberService;



    @GetMapping("/page")
    @Operation(summary = "获得客户群成员分页")
    @PreAuthorize("@ss.hasPermission('wecom:we-chat-group:query')")
    public CommonResult<PageResult<WeChatGroupMemberRespVO>> getWeChatGroupMemberPage(@Valid WeChatGroupMemberPageReqVO pageReqVO) {
        PageResult<WeChatGroupMemberDO> pageResult = weChatGroupMemberService.getWeChatGroupMemberPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WeChatGroupMemberRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得客户群成员分页")
    @Parameter(name = "weChatId", description = "企微群id")
    @PreAuthorize("@ss.hasPermission('wecom:we-chat-group:query')")
    public CommonResult<List<WeChatGroupMemberRespVO>> getWeChatGroupMemberListByChatId(@RequestParam("weChatId") String weChatId) {
        WeChatGroupMemberPageReqVO pageReqVO = new WeChatGroupMemberPageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        pageReqVO.setChatId(weChatId);
        List<WeChatGroupMemberDO> pageResult = weChatGroupMemberService.getWeChatGroupMemberPage(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, WeChatGroupMemberRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出客户群成员 Excel")
    @PreAuthorize("@ss.hasPermission('wecom:we-chat-group:export')")
    @OperateLog(type = EXPORT)
    public void exportWeChatGroupMemberExcel(@Valid WeChatGroupMemberPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WeChatGroupMemberDO> list = weChatGroupMemberService.getWeChatGroupMemberPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户群成员.xls", "数据", WeChatGroupMemberRespVO.class,
                        BeanUtils.toBean(list, WeChatGroupMemberRespVO.class));
    }

}